package com.woniu.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.woniu.entity.CreOrder;
import com.woniu.entity.CreRefund;
import com.woniu.entity.CreUser;
import com.woniu.service.CreditOrderService;
import com.woniu.service.CreditUserService;
import com.woniu.tool.MailProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Controller
 *
 * @author 陈泓霖
 * @date 2022-09-21
 */
@RestController
@Api("订单管理")
@RequestMapping("/credit/order")
public class CreditOrderController extends BaseController {
    @Autowired
    private CreditOrderService creOrderService;
    @Autowired
    private CreditUserService creditUserService;

    @Autowired
    private MailProperties mailProperties;


    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ApiOperation("查询订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单编号", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId", value = "用户id(外键)", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "realName", value = "用户名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "productId", value = "产品id(外键)", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "productName", value = "产品名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "commitTime", value = "提交时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "applymoney", value = "申请金额", dataType = "double", dataTypeClass = Double.class),
            @ApiImplicitParam(name = "loanTerm", value = "贷款期限(查字典)", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentMethod", value = "还款方式", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditor", value = "还款状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditStatus", value = "还款金额", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderStatus", value = "审核人", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "create_time", value = "审核状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "update_time", value = "订单状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "creRefund", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "creRefund", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "creRefund", value = "还款表信息", dataType = "CreRefund", dataTypeClass = CreRefund.class),
    })
    public TableDataInfo list(@RequestBody CreOrder creOrder) {
        startPage();
        List<CreOrder> list = creOrderService.selectCreOrderList(creOrder);
        return getDataTable(list);
    }

    @PostMapping("/verifInfo")
    @ApiOperation("验证个人信息生成订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "realName", value = "用户名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "productId", value = "产品id", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "productName", value = "产品名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "commitTime", value = "提交时间", dataType = "Date", dataTypeClass = String.class),
            @ApiImplicitParam(name = "applymoney", value = "申请金额", dataType = "double", dataTypeClass = Double.class),
            @ApiImplicitParam(name = "loanTerm", value = "贷款期限", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentMethod", value = "还款方式", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentStatus", value = "还款状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditor", value = "审核人", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "create_time", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "update_time", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "creRefund", value = "还款表信息", dataType = "CreRefund", dataTypeClass = CreRefund.class),

    })
    public AjaxResult verifInfo(@RequestBody CreOrder creOrder) {
        long userId = SecurityUtils.getUserId();
        int id = (int) userId;
//        System.out.println(id);
//        System.out.println(11111111);
        SysUser sysUser = creditUserService.findSysUserById(id);
        CreUser creUser = creditUserService.findUserById(id);
        System.out.println(creUser);
        if (creOrder.getApplymoney() == 0) {
            return AjaxResult.success("贷款金额未填写,请重新填写!", 5001);
        } else {
            if (sysUser.getEmail() != null && creUser.getIdentityCard() != null
                    && creUser.getBankCard() != null && creUser.getRealName() != null &&
                    creUser.getEmergencyPhone() != null
            ) {
//                System.out.println(2222222);
//                System.out.println(creOrder);
                CreOrder c = new CreOrder(0, id, creUser.getRealName(), creOrder.getProductId(), creOrder.getProductName(),
                        null, creOrder.getApplymoney(), creOrder.getLoanTerm(), creOrder.getPaymentMethod(), "0", null, "0", "0", null, null, null);
                int rows = creOrderService.addOrder(c);
                return AjaxResult.success("新增订单成功!", rows);
            } else {

                return AjaxResult.success("个人信息不完整,请重新填写!", 5000);
            }
        }

    }

    /**
     * 获取详细信息
     */
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(creOrderService.selectCreOrderByOrderId(orderId));
    }

    /**
     * 获取单个订单实体
     */
    @PostMapping("/byOrderId")

    public AjaxResult getOrderByOrderId(@RequestBody Integer orderId) {
        return AjaxResult.success(creOrderService.selectOrderByOrderId(orderId));
    }

    /**
     * 新增
     */
    @PostMapping
    @ApiOperation("新增订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单编号", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId", value = "用户id(外键)", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "realName", value = "用户名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "productId", value = "产品id(外键)", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "productName", value = "产品名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "commitTime", value = "提交时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "applymoney", value = "申请金额", dataType = "double", dataTypeClass = Double.class),
            @ApiImplicitParam(name = "loanTerm", value = "贷款期限(查字典)", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentMethod", value = "还款方式", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditor", value = "还款状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditStatus", value = "还款金额", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderStatus", value = "审核人", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "create_time", value = "审核状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "update_time", value = "订单状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "creRefund", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "creRefund", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "creRefund", value = "还款表信息", dataType = "CreRefund", dataTypeClass = CreRefund.class),
    })
    public AjaxResult add(@RequestBody CreOrder creOrder) {
        return toAjax(creOrderService.insertCreOrder(creOrder));
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单编号", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId", value = "用户id(外键)", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "realName", value = "用户名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "productId", value = "产品id(外键)", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "productName", value = "产品名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "commitTime", value = "提交时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "applymoney", value = "申请金额", dataType = "double", dataTypeClass = Double.class),
            @ApiImplicitParam(name = "loanTerm", value = "贷款期限(查字典)", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentMethod", value = "还款方式", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditor", value = "还款状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditStatus", value = "还款金额", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderStatus", value = "审核人", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "create_time", value = "审核状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "update_time", value = "订单状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "creRefund", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "creRefund", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "creRefund", value = "还款表信息", dataType = "CreRefund", dataTypeClass = CreRefund.class),
    })
    public AjaxResult edit(@RequestBody CreOrder creOrder) {
        return toAjax(creOrderService.updateCreOrder(creOrder));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{orderIds}")
    @ApiOperation("修改订单")
    @ApiImplicitParam(name = "orderIds", value = "订单编号数组", dataType = "Long", dataTypeClass = Long.class)
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(creOrderService.deleteCreOrderByOrderIds(orderIds));
    }

    /**
     * 指定用户订单
     *
     * @param creOrder
     * @return
     */
    @ApiOperation("指定用户订单")
    @PostMapping("/userOrderList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单编号", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId", value = "用户id(外键)", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "realName", value = "用户名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "productId", value = "产品id(外键)", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "productName", value = "产品名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "commitTime", value = "提交时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "applymoney", value = "申请金额", dataType = "double", dataTypeClass = Double.class),
            @ApiImplicitParam(name = "loanTerm", value = "贷款期限(查字典)", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentMethod", value = "还款方式", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditor", value = "还款状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditStatus", value = "还款金额", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderStatus", value = "审核人", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "create_time", value = "审核状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "update_time", value = "订单状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "creRefund", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "creRefund", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "creRefund", value = "还款表信息", dataType = "CreRefund", dataTypeClass = CreRefund.class),
    })
    public TableDataInfo userOrderList(@RequestBody CreOrder creOrder) {
        startPage();
        List<CreOrder> list = creOrderService.userOrderList(creOrder);
        return getDataTable(list);
    }

    /**
     * 单次还款金额测算
     * 当前登录者
     */
    @ApiOperation("单次还款金额测算")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "int", dataTypeClass = Integer.class)
    @GetMapping("/getRefund/{orderId}")
    public AjaxResult getRefund(@PathVariable int orderId) {
        return creOrderService.getRefund(orderId);
    }

    /**
     * 一次性还款金额测算
     * 当前登录者
     */
    @ApiOperation("一次性还款金额测算")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "int", dataTypeClass = Integer.class)
    @GetMapping("/getDisRefundById/{orderId}")
    public AjaxResult getDisRefundById(@PathVariable int orderId) {
        return creOrderService.getDisRefundById(orderId);
    }


    /**
     * 用户下载自己的订单
     */
    @ApiOperation("用户下载自己的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "订单ID", dataType = "int", dataTypeClass = Integer.class)
    })
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreOrder creOrder) {
        List<CreOrder> list = creOrderService.userOrderList(creOrder);
        ExcelUtil<CreOrder> excelUtil = new ExcelUtil<>(CreOrder.class);
        excelUtil.exportExcel(response, list, creOrder.getRealName() + "的用户数据");
    }

    @GetMapping("/sendText/{to}/{subject}/{text}")
    public void sendText(@PathVariable("to") String to, @PathVariable("subject") String subject, @PathVariable("text") String text) {
        mailProperties.sendMail(to, subject, text);

    }
}
